package visiopuce.ui.listener;

import java.util.Iterator;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import visiopuce.objets.Analyse;
import visiopuce.service.PersonneServiceImpl;
import visiopuce.ui.DossierEditorInput;
import visiopuce.ui.view.DossierEditor;

public class DoubleCliqueListeTravailListener implements IDoubleClickListener {

	public void doubleClick(DoubleClickEvent event) {
		// if the selection is empty clear the label
		if (event.getSelection().isEmpty()) {
			System.out.println("la selection dans le tableau est vide");
			return;
		}
		if (event.getSelection() instanceof IStructuredSelection) {
			IStructuredSelection selection = (IStructuredSelection) event.getSelection();
			selection.getFirstElement();
			StringBuffer toShow = new StringBuffer();
			for (Iterator iterator = selection.iterator(); iterator.hasNext();) {
				try {
					Analyse analyse = (Analyse) iterator.next();

					IEditorInput editorInput = new DossierEditorInput(PersonneServiceImpl.getInstance().getIdPersonneFromObject(analyse));
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(editorInput, DossierEditor.ID, true, IWorkbenchPage.MATCH_INPUT);
					IEditorPart editeurActif = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
					if (editeurActif instanceof DossierEditor) {
						DossierEditor editeur = (DossierEditor) editeurActif;
						if (editeur.getAnalyseTabItems().containsKey(analyse.getIdAnalyse())) {
							editeur.getTabFolder().setSelection(editeur.getAnalyseTabItems().get(analyse.getIdAnalyse()).getTabItem());
						} else {
							editeur.addAnalyseTabItem(analyse);

						}
					}

				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

	}
}
